<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<%@ taglib prefix="redis" uri="http://www.shopjsp.com/redisTag/functions" %>
<c:set var="basePath" value="${redis:getSysConfig('base_path_back')}"/>
<c:set var="uploadPath" value="${redis:getSysConfig('uploadFileVisitRoot')}"/>
<script type="text/javascript">
    /**
     * 自定义验证规则
     */
    function validateForm_discount() {
        $("#form_promotionDiscount").bootstrapValidator({
            message: "无效的值",
            feedbackIcons: {
                valid: "glyphicon glyphicon-ok",
                invalid: "glyphicon glyphicon-remove",
                validating: "glyphicon glyphicon-refresh"
            },
            fields: {
                discount: {
                    validators: {
                        notEmpty: {message: "折扣率不可为空"},
                        regexp: {
                            regexp: validateUtil.discount,
                            message: '格式必须为 *.* ! 如:5.6'
                        }
                    }
                }
            }
        });
    }

    /**
     * 维护活动折扣
     */
    <shiro:hasPermission name="back_gotoPromotionPage_promotionDiscount">
    function maintainPromotionDiscount() {
        /**获取选中行的id**/
        var selectedRow = $("#tt").bootstrapTable('getSelections');
        if (selectedRow.length == 1) {
            if(selectedRow[0].isPass==2){
                $("#btn_maintainPromotionDiscount").attr("data-target", "");/**选中了记录后才可打开对应的模态框**/
                showMsg("已通过审核的活动不能维护折扣！");
            }else{
				$("#btn_maintainPromotionDiscount").attr("data-target", "#promotionDiscountModal");/**选中了记录后才可打开对应的模态框**/
                $.ajax({
                    type: "POST",
                    dataType: "JSON",
                    url: "${basePath}/back/promotion/getSalesPromotionDiscountObject.sjson",
                    data: {promotionId: selectedRow[0].promotionId},
                    success: function (result) {
                        var data = JSON.parse(result.data);
                        if(data!=null){
                            /**隐藏域**/
                            $("#disproductId").val(data.disproductId);
                            $("#promotionId_promotionDiscount").val(data.promotionId);
                            $("#promotionIdNumber_promotionDiscount").val(data.promotionIdNumber);
                            /**可见表单控件**/
                            $("#discount").val(data.discount);
                        }else{
                            /**清空隐藏域**/
                            $("#disproductId").val("");
                            $("#promotionIdNumber_promotionDiscount").val("");
                            $("#promotionId_promotionDiscount").val(selectedRow[0].promotionId);
                            /** 重置添加表单**/
                            $("#form_promotionDiscount")[0].reset();
                        }
                    }
                });
            }
        }else {
            $("#btn_maintainPromotionDiscount").attr("data-target", "");/**选中了记录后才可打开对应的模态框**/
            showMsg("请选择一行记录！");
        }
    }
    </shiro:hasPermission>

    /**
     * 表单提交及验证初始化
     */
    $(document).ready(function() {
        if($("#form_promotionDiscount").length>0){ /**判断当前页面是否有填写表单并检验**/
        	validateForm_discount();
            /**Modal验证销毁重构**/
            $('#addOrEditModal').on('hidden.bs.modal', function() {
                reValidateForm_discount();
            });
        }
    });

    /**
     * 重置表单验证
     */
    function reValidateForm_discount(){
        $("#form_promotionDiscount").data('bootstrapValidator', null);
        validateForm_discount();
    }

    /**
     * 表单异步提交
     */
    var isValid;
    function submitForm_promotionDiscount(){
        /** 开启验证**/
        $("#form_promotionDiscount").data('bootstrapValidator').validate();
        isValid=$("#form_promotionDiscount").data('bootstrapValidator').isValid();
        if(isValid){  /** 验证通过后提交表单**/
        	var options = {
                url: $("#form_promotionDiscount").attr("action"),
                type : "post",
                dataType:"json",
                success : function(data) {
                    if(data.success==true){
                        /** 先重置查询框的值**/
                        resetQueryForm();
                        /** 重新刷新列表数据**/
                        $("#tt").bootstrapTable('refresh');
                        /** 自动关闭弹出模态框窗口**/
                        $(".close").click();
                        showMsg("保存成功！");
                    }else{
                        showMsg("保存失败！");
                    }
                }
            };
            $("#form_promotionDiscount").ajaxSubmit(options);
        }else{ /** 验证失败了直接返回 **/

        }
    }
</script>

<!--活动折扣模态框（Modal） -->
<div class="modal fade" id="promotionDiscountModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static">
	<div class="modal-dialog" style="width:70%">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				<h5 class="modal-title" id="myModalLabel">维护活动折扣</h5>
			</div>
			<div class="modal-body">
				<form id="form_promotionDiscount" method="post" action="${basePath}/back/promotion/saveOrUpdateSalesPromotionDiscount.sjson">
					<input id="disproductId" type="hidden" name="disproductId" />
					<input id="promotionId_promotionDiscount" type="hidden" name="promotionId" />
					<input id="promotionIdNumber_promotionDiscount" type="hidden" name="promotionIdNumber"/>
					<table align="center" class="addOrEditTable table-bordered table" style="width:100%;">
						<tr>
							<td class="toright_td" width="150px"><span style="color:red">* </span>折扣率:<span>（折）</span>&nbsp;&nbsp;</td>
							<td class="toleft_td">
								<div class="form-group">
									<input type="text" class="form-control input-sm" id="discount" name="discount" placeholder="请输入折扣率"/>
								</div>
							</td>
						</tr>
					</table>
				</form>
			</div>
			<div class="modal-footer">
				<button type="button" id="validateBtn" class="btn btn-primary" onclick="submitForm_promotionDiscount()">保存</button>
				<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
			</div>
		</div><!-- /.modal-content -->
	</div><!-- /.modal -->
</div>
